package com.gitee.ice1938.module.admin.core.operator.fun;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.datatables.ajax.FormData;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gitee.ice1938.admin.framwork.annotation.ReqOperation;
import com.gitee.ice1938.module.admin.framwork.core.MenuItem;
import com.gitee.ice1938.mvc.base.AjaxResult;
import com.gitee.ice1938.mvc.base.PageConvert;

/**
 * 系统功能配置
 * 
 * @author ice1938@qq.com 2019-10-25
 *
 */
@Controller
@RequestMapping("/admin/operator")
public class OperatorController {

	@Autowired
	private OperatorService operatorService;

	/**
	 * 测试页面
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "init", method = RequestMethod.POST)
	@ResponseBody
	@ReqOperation(ignore = true)
	public AjaxResult init(HttpServletRequest request, HttpServletResponse response, Model model) {
		operatorService.initOperator();
		return AjaxResult.ok();
	}

	/**
	 * 页面索引
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		FormData formData = new FormData(request);
		Pageable pagebale = PageConvert.convertPageable(formData);
		Order order = PageConvert.convertOrder(formData);
		Page<MenuItem> data = operatorService.queryData(formData.getParam(), pagebale, order);
		model.addAttribute("operators", data);
		return "admin/view_operator";
	}

	/**
	 * 页面数据查询方法
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "search", method = RequestMethod.POST)
	@ResponseBody
	public Page<MenuItem> findData(HttpServletRequest request, HttpServletResponse response, Model model) {
		FormData formData = new FormData(request);
		Pageable pagebale = PageConvert.convertPageable(formData);
		Order order = PageConvert.convertOrder(formData);
		Page<MenuItem> data = operatorService.queryData(formData.getParam(), pagebale, order);
		return data;

	}

	@RequestMapping(path = "save", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult saveOption(@RequestParam("id") String id, @RequestParam("position") Integer position,
	        @RequestParam(value = "parent_id", required = false) String parentId,
	        @RequestParam("icon_class") String iconClass) {
		
		int flag = operatorService.update(id, parentId, iconClass, position);
		AjaxResult result = new AjaxResult("更新成功!", 200);
		if (flag == 0) {
			result = new AjaxResult("未更新数据!", 200);
		}
		return result;
	}
}
